import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi import Request
from pathlib import Path
import tomllib

from api.v1 import agent_router, execution_record_router, task_router

app = FastAPI()


# 添加CORS中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(agent_router, prefix="/api/v1")
app.include_router(execution_record_router, prefix="/api/v1")
app.include_router(task_router, prefix="/api/v1")


from app.config import config



@app.exception_handler(Exception)
async def generic_exception_handler(request: Request, exc: Exception):
    return JSONResponse(
        status_code=500, content={"message": f"Server error: {str(exc)}"}
    )

if __name__ == "__main__":
    uvicorn.run(app, host=config.server.host, port=config.server.port)
